package com.zzyq.pc.mongodb.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.zzyq.pc.common.StandardDataFormat;
import com.zzyq.pc.common.base.BaseController;
import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.mongodb.Service.IWarningService;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.utils.DateSyncUtil;
import com.zzyq.pc.utils.HttpUtils;
import com.zzyq.pc.utils.LoggerUtils;

@Controller
@RequestMapping(value="Warning",produces="application/json;charset=UTF-8")
public class WarningController extends BaseController{

	@Autowired
	IWarningService warningService;
	
	
	/**
	 * 舆情预警——首页
	 */
	@ResponseBody
	@RequestMapping("findWarning")
	public String findWarning(HttpServletRequest request,HttpServletResponse response,Integer greaterForward
			,String timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Date time = null;
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		try {
			//时间范围
			if (StringUtils.isNotBlank(timeScope) || !timeScope.isEmpty()) {
				time = DateSyncUtil.parse(timeScope);
			}
			list = warningService.findWarning(greaterForward,time);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 高危舆情——首页
	 */
	@ResponseBody
	@RequestMapping("findRisk")
	public String findRisk(HttpServletRequest request,HttpServletResponse response,Integer greaterForward
			,Integer lessForward,String timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		Date time = null;
		try {
			//时间范围
			if (StringUtils.isNotBlank(timeScope) || !timeScope.isEmpty()) {
				time = DateSyncUtil.parse(timeScope);
			}
			//long l = System.currentTimeMillis();
			list = warningService.findRisk(greaterForward,lessForward,time);
			//System.out.println(System.currentTimeMillis()-l);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 负面舆情——首页
	 */
	@ResponseBody
	@RequestMapping("findNegative")
	public String findNegative(HttpServletRequest request,HttpServletResponse response,Integer lessForward
			,String timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		Date time = null;
		try {
			//时间范围
			if (StringUtils.isNotBlank(timeScope) || !timeScope.isEmpty()) {
				time = DateSyncUtil.parse(timeScope);
			}
			list = warningService.findNegative(lessForward,time);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 最新舆情——首页
	 * @param userId
	 */
	@ResponseBody
	@RequestMapping("findNewest")
	public String findNewest(HttpServletRequest request,HttpServletResponse response,String timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		Date time = null;
		try {
			if (StringUtils.isNotBlank(timeScope) || !timeScope.isEmpty()) {
				time = DateSyncUtil.parse(timeScope);
			}
			list = warningService.findNewest(time);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	/**
	 * 舆情预警——更多
	 * @param userId
	 */
	@ResponseBody
	@RequestMapping("findWarningMore")
	public String findWarningMore(HttpServletRequest request,HttpServletResponse response,Integer pageIndex,
			Integer pageSize,Integer greaterForward){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		PageView pageView = null;
		try {
			if (greaterForward <= 0 || greaterForward == null) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (null == pageIndex || pageIndex <= 0) {
				pageIndex=1;
			}
			if (null == pageSize || pageSize <= 0) {
				pageSize=15;
			}
			pageView = new PageView(pageSize,pageIndex);
			//long l = System.currentTimeMillis();
			list = warningService.findWarningMore(pageView,greaterForward);
			//System.out.println(System.currentTimeMillis()-l);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 负面舆情——更多
	 * @param userId
	 */
	@ResponseBody
	@RequestMapping("findNegativeMore")
	public String findNegativeMore(HttpServletRequest request,HttpServletResponse response,Integer pageIndex,
			Integer pageSize,Integer lessForward){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		PageView pageView = null;
		try {
			if (lessForward <= 0 || lessForward == null) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (null == pageIndex || pageIndex <= 0) {
				pageIndex=1;
			}
			if (null == pageSize || pageSize <= 0) {
				pageSize=15;
			}
			pageView = new PageView(pageSize,pageIndex);
			//long l = System.currentTimeMillis();
			list = warningService.findNegativeMore(pageView,lessForward);
			//System.out.println(System.currentTimeMillis()-l);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 最新舆情——更多
	 * @param userId
	 */
	@ResponseBody
	@RequestMapping("findNewestMore")
	public String findNewestMore(HttpServletRequest request,HttpServletResponse response,Integer pageIndex,
			Integer pageSize){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		PageView pageView = null;
		try {
			if (null == pageIndex || pageIndex <= 0) {
				pageIndex=1;
			}
			if (null == pageSize || pageSize <= 0) {
				pageSize=15;
			}
			pageView = new PageView(pageSize,pageIndex);
			//long l = System.currentTimeMillis();
			list = warningService.findNewestMore(pageView);
			//System.out.println(System.currentTimeMillis()-l);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	
	/**
	 * 高危舆情——更多
	 * @param pageIndex
	 * @param pageSize
	 */
	@ResponseBody
	@RequestMapping("findHighRiskMorer")
	public String findHighRiskMorer(HttpServletRequest request,HttpServletResponse response,Integer pageIndex,
			Integer pageSize,Integer greaterForward,Integer lessForward){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		PageView pageView = null;
		try {
			if (greaterForward <= 0 || greaterForward == null && lessForward <= 0 || lessForward == null) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (null == pageIndex || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (null == pageSize || pageSize <= 0) {
				pageSize = 15;
			}
			pageView = new PageView(pageSize, pageIndex);
			//long l = System.currentTimeMillis();
			list = warningService.findHighRiskMore(pageView,greaterForward,lessForward);
			//System.out.println(System.currentTimeMillis()-l);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	/**
	 * 查询相似数据
	 * @param groupId
	 * @param ifmIds
	 */
	@ResponseBody
	@RequestMapping("findSimilarity")
	public String findSimilarity(HttpServletRequest request,HttpServletResponse response,String groupId,
			String articleType,String articleType2,Integer identifying,String startDate,String endDate,
			String ifmId){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		Date endTime1 = null;
		Date startTime1 = null;
		List<String> idlist = new ArrayList<String>();
		try {
			if (StringUtils.isBlank(groupId)) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (identifying != 1 && identifying != 2 && identifying != 3) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				endTime1 = DateSyncUtil.parse(endDate);
				startTime1 = DateSyncUtil.parse(startDate);
			}
			if (StringUtils.isNotBlank(articleType)) {
				idlist = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(articleType);
			}
			//查询监控媒体数据
			if (StringUtils.isNotBlank(articleType2)) {
				articleType2 = transString(request, articleType2);
			}
			list = warningService.findSimilarity(groupId,idlist,articleType2,identifying,endTime1,startTime1,ifmId);
			return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	
	
}
